<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        list-style: none;
      }
      .nav {
        display: flex;
      }
      .nav li {
        width: 100px;
        height: 40px;
        line-height: 40px;
        text-align: center;
        background-color: #ccc;
      }
      .nav .sty {
        background-color: aqua;
        color: #fff;
      }
      .content li {
        margin: 10px 0;
        width: 500px;
        padding: 10px 20px;
        background-color: rgb(235, 235, 235);
        font-size: 14px;
        font-weight: 600;
      }
      .ipt {
        margin: 10px 0;
        width: 500px;
        padding: 10px 20px;
        background-color: rgb(235, 235, 235);
      }
      .ipt input {
        width: 260px;
        height: 30px;
        margin: 5px 0;
      }
      .ipt button {
        width: 80px;
        height: 30px;
        background-color: #ccc;
        border: none;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <Nav-Tap></Nav-Tap>
      <Guan v-show="show==0"></Guan>
      <Fa-Bu v-show="show==1"></Fa-Bu>
    </div>

    <!-- tap栏切换组件 -->
    <template id="nav">
      <div>
        <ul class="nav">
          <li :class="{sty:isShow==0}" @click="isShow=0,$root.show=0">
            评论管理
          </li>
          <li :class="{sty:isShow==1}" @click="isShow=1,$root.show=1">
            发布评论
          </li>
        </ul>
      </div>
    </template>
    <!-- 评论管理组件 -->
    <template id="guan">
      <div>
        <ul class="content">
          <li v-for="(item,index) in list">
            <p>评论人：{{item.name}}</p>
            <p>评论内容：{{item.content}}</p>
            <p style="font-size: 12px">评论时间：{{item.time}}</p>
            <button @click="list.splice(index,1)">删除</button>
          </li>
        </ul>
      </div>
    </template>
    <!-- 发布组件 -->
    <template id="fa">
      <div class="ipt">
        <p><input type="text" v-model.trim="name" placeholder="昵称" /></p>
        <p>
          <input type="text" v-model.trim="content" placeholder="评论内容" />
        </p>
        <button @click="faBu()">立即提交</button>
      </div>
    </template>
    <script src="./vue.js"></script>
    <script>
      // 事件总线
      const bus = new Vue();
      // 评论管理组件
      const Guan = {
        template: "#guan",
        data() {
          return {
            list: [],
          };
        },
        mounted() {
          bus.$on("send", (data) => {
            this.list.push({ ...data, time: getTime(), id: Date.now() });
          });
        },
      };
      //   发布评论组件
      const FaBu = {
        template: "#fa",
        data() {
          return {
            name: "",
            content: "",
          };
        },
        methods: {
          faBu() {
            if (this.name && this.content) {
              bus.$emit("send", { name: this.name, content: this.content });
              this.name = "";
              this.content = "";
            }
          },
        },
      };
      // tap栏切换组件
      const NavTap = {
        template: "#nav",
        data() {
          return {
            isShow: 0,
          };
        },
      };
      // 根
      new Vue({
        el: "#app",
        data: {
          show: 0, //显示的组件
        },
        components: {
          NavTap,
          Guan,
          FaBu,
        },
      });
      //获取当前时间
      function getTime() {
        let date = new Date();
        let nowTime =
          date.getFullYear() +
          "-" +
          (date.getMonth() + 1) +
          "-" +
          date.getDate() +
          "  " +
          (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) +
          ":" +
          (date.getMinutes() < 10
            ? "0" + date.getMinutes()
            : date.getMinutes());
        return nowTime;
      }
    </script>
  </body>
</html>
